﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GameServer;

namespace Rappelz.GameServer
{
    public class XLog
    {
        private String baseName;

        public enum Levels
        {
            Trace,
            Debug,
            Info,
            Warning,
            Error,
            Fatal
        }

        public Levels Level = XLog.Levels.Debug;
        public Levels guiLogLevel;

        public XLog(String name)
        {
            baseName = name;
            guiLogLevel = Levels.Info;
        }

        private void log(Levels logLevel, String logString)
        {
            String logLevelStr;

            lock (this)
            {
                //if current Level is higher than the request log text, skip
                if (logLevel < Level)
                    return;

                #region logLevelStr switch
                switch (logLevel)
                {
                    case Levels.Debug:
                        logLevelStr = "Debug";
                        break;

                    case Levels.Error:
                        logLevelStr = "Error";
                        break;

                    case Levels.Fatal:
                        logLevelStr = "Fatal";
                        break;

                    case Levels.Info:
                        logLevelStr = "Info";
                        break;

                    case Levels.Trace:
                        logLevelStr = "Trace";
                        break;

                    case Levels.Warning:
                        logLevelStr = "Warning";
                        break;

                    default:
                        logLevelStr = "";
                        break;
                }
                #endregion

                String fullStr = String.Format("{0}-{1:00}-{2:00} {3:00}:{4:00}.{5:00} {6}: {7}\r\n", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                    DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second,
                    logLevelStr, logString);

                //write the log string to file
                String filename = String.Format("{0}_{1}-{2:00}-{3:00}.log", baseName, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Append, System.IO.FileAccess.Write);
                System.IO.StreamWriter tw = new System.IO.StreamWriter(fs);

                tw.Write(fullStr);
                tw.Close();
                fs.Close();
                fs.Dispose();
                fs = null;


                //if logLevel > Info, show it on the GUI
                if (logLevel >= guiLogLevel && formGameServer.Instance != null)
                    formGameServer.Instance.addLogLine(fullStr);
            }
        }

        private void log(Levels logLevel, String logString, params object[] args)
        {
            log(logLevel, String.Format(logString, args));
        }

        public void Trace(String l)
        {
            log(Levels.Trace, l);
        }

        public void Trace(String l, params object[] args)
        {
            log(Levels.Trace, l, args);
        }


        public void Debug(String l)
        {
            log(Levels.Debug, l);
        }

        public void Debug(String l, params object[] args)
        {
            log(Levels.Debug, l, args);
        }

        public void Log(String l)
        {
            log(Levels.Debug, l);
        }

        public void Log(String l, params object[] args)
        {
            log(Levels.Debug, l, args);
        }

        public void Info(String l)
        {
            log(Levels.Info, l);
        }

        public void Info(String l, params object[] args)
        {
            log(Levels.Info, l, args);
        }

        public void Warning(String l)
        {
            log(Levels.Warning, l);
        }

        public void Warning(String l, params object[] args)
        {
            log(Levels.Warning, l, args);
        }

        public void Error(String l)
        {
            log(Levels.Error, l);
        }

        public void Error(String l, params object[] args)
        {
            log(Levels.Error, l, args);
        }

        public void Fatal(String l)
        {
            log(Levels.Fatal, l);
        }

        public void Fatal(String l, params object[] args)
        {
            log(Levels.Fatal, l, args);
        }
    }
}
